//
//  PXHGetNetworkStatus.m
//  Courses
//
//  Created by YiXuePxh on 15/5/27.
//  Copyright (c) 2015年 boyibang. All rights reserved.
//

#import "PXHGetNetworkStatus.h"

@implementation PXHGetNetworkStatus
+(BOOL)isHaveNetWork{
    BOOL netWork;
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
    NSString *state = [[NSString alloc]init];
    int netType = 0;
    //获取到网络返回码
    for (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            //获取到状态栏
            netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
            
            switch (netType) {
                case 0:
                    state = @"noNetWork";
                    //无网模式
                    break;
                case 1:
                    state = @"2G";
                    break;
                case 2:
                    state = @"3G";
                    break;
                case 3:
                    state = @"4G";
                    break;
                case 5:
                {
                    state = @"WIFI";
                }
                    break;
                default:
                    break;
            }
        }
    }
    NSLog(@"当前网络状态：%@",state);
    //根据状态选择
    if ([state isEqualToString:@"noNetWork"]) {
        netWork = true;
    }else{
        netWork = false;
    }
    return netWork;
}
+(BOOL)isWifi{
    BOOL isWifi;
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
    NSString *state = [[NSString alloc]init];
    int netType = 0;
    //获取到网络返回码
    for (id child in children) {
        if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
            //获取到状态栏
            netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
            
            switch (netType) {
                case 0:
                    state = @"noNetWork";
                    //无网模式
                    break;
                case 1:
                    state = @"2G";
                    break;
                case 2:
                    state = @"3G";
                    break;
                case 3:
                    state = @"4G";
                    break;
                case 5:
                {
                    state = @"WIFI";
                }
                    break;
                default:
                    break;
            }
        }
    }
    NSLog(@"当前网络状态：%@",state);
    //根据状态选择
    BOOL justWifi = [[[NSUserDefaults standardUserDefaults]objectForKey:@"justWifi"]boolValue];
     NSLog(@"当前网络状态：%d",justWifi);
    if (justWifi) {
        if ([state isEqualToString:@"noNetWork"]) {
            isWifi = false;
        }else{
            if ([state isEqualToString:@"WIFI"]) {
                isWifi = true;
            }else{
                isWifi = false;
            }
        }
    }else{
        if ([state isEqualToString:@"3G"]||[state isEqualToString:@"4G"]||[state isEqualToString:@"WIFI"]) {
            isWifi = true;
        }else{
            isWifi = false;
        }
    }
    return isWifi;
}
@end
